//
//  WYCAddPayViewController.m
//  51信用卡
//
//  Created by 王晓民 on 2018/3/14.
//  Copyright © 2018年 中企互联. All rights reserved.
//

#import "WYCAddPayViewController.h"

@interface WYCAddPayViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic,strong) UIImageView *wxImageView;
@property(nonatomic,strong) UITextField * userTextField;
@end

@implementation WYCAddPayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = WYCGlobalBg;

    
    UIView  *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, NavHeight, ScreenWidth, 300)];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.userInteractionEnabled = YES;
    [self.view addSubview:bgView];
   _userTextField = [[UITextField alloc] init];
    _userTextField.frame = CGRectMake(20 * px, 10, ScreenWidth-20, 20);
    _userTextField.backgroundColor = [UIColor clearColor];
    _userTextField.placeholder = @"备注收款码名称（10字以内）";
    [bgView addSubview:_userTextField];
    UIImageView  *weixin = [[UIImageView alloc]initWithFrame:CGRectMake(10, 130, 170, 170)];
    weixin.image = [UIImage imageNamed:@"erweima"];
    weixin.userInteractionEnabled = YES;
    [weixin addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pressZf:)]];
    
    [self.view addSubview:weixin];
    self.wxImageView = weixin;
    
    UILabel  *wxLb = [[UILabel alloc]initWithFrame:CGRectMake(10, weixin.frame.size.height+weixin.frame.origin.y+10, 175, 20)];
    wxLb.text = @"添加收款码";
    wxLb.textColor = [UIColor blackColor];
    wxLb.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:wxLb];
}
-(void)pressZf:(UITapGestureRecognizer *)tap
{
    
    UIAlertController *aler=[UIAlertController alertControllerWithTitle:@"" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    //从相机选取
    UIAlertAction *album=[UIAlertAction actionWithTitle:@"从相册选取" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController *picker=[[UIImagePickerController alloc]init];
        picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
        picker.allowsEditing = YES;//设置照片可编辑
        
        picker.mediaTypes=[UIImagePickerController availableMediaTypesForSourceType:picker.sourceType];
        picker.allowsEditing=YES;
        picker.delegate=self;
        [self presentViewController:picker animated:YES completion:nil];
    }];
    
    UIAlertAction *cancl=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [aler addAction:cancl];
    [aler addAction:album];
    [self presentViewController:aler animated:YES completion:nil];
}
//选取图片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    UIImage *image1=[info objectForKey:UIImagePickerControllerEditedImage];
    
        self.wxImageView.image = image1;
    //选取完图片之后关闭视图
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)request{
    if(self.userTextField.text.length == 0)
    {
        [self showToast:@"备注名称格式不正确"];
        return;
    }
    else if (self.userTextField.text.length >10 )
    {
        [self showToast:@"备注名称应10字以内"];
        return;
    }
    else if (!self.wxImageView.image)
    {
        [self showToast:@"请上传收款码"];
        return;
    }
    
    NSData * data = UIImageJPEGRepresentation(self.wxImageView.image, 0.0000001);
    NSString *encodedImageStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"token"] = [WYCAccountTool unarchivetoken];
    params[@"base64Img"] = encodedImageStr;
    params[@"name"] = _userTextField.text;
    NSLog(@"%@",params);
    [HttpTool POST:saveUserCode params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"收款码%@",responseObject);
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            
            [self showToast:msgStr];
            [self.navigationController popToRootViewControllerAnimated:YES];
            
        }else{
            
            [self showToast:msgStr];
            
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [self showToast:Failed];
    }];
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    UIView * NavView = [[UIView alloc] init];
    NavView.frame = CGRectMake(0, 0, ScreenWidth, NavHeight);
    NavView.backgroundColor = WYCmainColor;
    [self.view addSubview:NavView];
    
    CALayer *bottomLineLayer = [[CALayer alloc] init];
    bottomLineLayer.frame = CGRectMake(0, NavView.bounds.size.height - 0.5, NavView.bounds.size.width, 0.5);
    bottomLineLayer.backgroundColor = WYCmainColor.CGColor;
    [NavView.layer addSublayer:bottomLineLayer];
    //返回按钮
    UIButton *  fanhuibutton = [[UIButton alloc] init];
    [fanhuibutton setImage:[UIImage imageNamed:@"Nav_back"] forState:UIControlStateNormal];
    [fanhuibutton addTarget:self action:@selector(photoClick) forControlEvents:UIControlEventTouchUpInside];
    fanhuibutton.frame = CGRectMake(30 * px, titleY, 66 * px , 66 * px);
    [NavView addSubview:fanhuibutton];
    
    //返回按钮
    UIButton *  editbutton = [[UIButton alloc] init];
    [editbutton setTitle:@"保存" forState:UIControlStateNormal];
    [editbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [editbutton addTarget:self action:@selector(presSave) forControlEvents:UIControlEventTouchUpInside];
    editbutton.frame = CGRectMake(ScreenWidth-60, titleY, 40 , 20);
    [NavView addSubview:editbutton];
    
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
    NavRigsterLabel.frame = CGRectMake(0, titleY, NavView.width, 68 * px);
    NavRigsterLabel.textColor = [UIColor whiteColor];
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    NavRigsterLabel.text = @"添加收款码";
    [NavView addSubview:NavRigsterLabel];
}

-(void)presSave
{
    NSLog(@"保存");
    [self request];
}
- (void)photoClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}

@end
